package com.yuehuanghun.admin.core.controller.monitor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yuehuanghun.admin.core.config.util.DeptUtils;
import com.yuehuanghun.admin.core.config.util.UserUtils;
import com.yuehuanghun.admin.core.domain.SysLogininfor;
import com.yuehuanghun.admin.core.domain.SysUserOnline;
import com.yuehuanghun.framework.annotation.Log;
import com.yuehuanghun.framework.annotation.Log.BusinessType;
import com.yuehuanghun.framework.domain.PageDTO;
import com.yuehuanghun.framework.util.StringUtils;
import com.yuehuanghun.framework.web.BaseResource;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;

/**
 * 在线用户监控
 * 
 * @author yadmin
 */
@RestController
@RequestMapping("/monitor/online")
@SaCheckLogin
public class SysUserOnlineController extends BaseResource {

	@GetMapping("/list")
	@SaCheckPermission("monitor:online:list")
	public PageDTO<SysUserOnline> list(String ipaddr, String userName) {
		List<String> tokens = StpUtil.searchTokenSessionId("", -1, Integer.MAX_VALUE);

		List<SysUserOnline> userOnlineList = new ArrayList<>();
		for (String token : tokens) {
			SaSession session = StpUtil.getSessionBySessionId(token);
			if(session == null) {
				continue;
			}
			
			SysLogininfor loginInfo = (SysLogininfor)session.get("loginInfo");
			if(loginInfo == null) {
				continue;
			}
			//查询条件过滤
			if(StringUtils.isNotBlank(ipaddr) && (loginInfo.getIpaddr() == null || !loginInfo.getIpaddr().contains(ipaddr))) {
				continue;
			}
			//查询条件过滤
			if(StringUtils.isNotBlank(userName) && (loginInfo.getUserName() == null || !loginInfo.getUserName().contains(userName))) {
				continue;
			}
			
			SysUserOnline on = new SysUserOnline();
			on.setUserName(loginInfo.getUserName());
			on.setBrowser(loginInfo.getBrowser());
			on.setDeptName(DeptUtils.getDeptName(UserUtils.getUser(loginInfo.getUserName()).getDeptId()));
			on.setIpaddr(loginInfo.getIpaddr());
			on.setLoginLocation(loginInfo.getLoginLocation());
			on.setLoginTime(loginInfo.getLoginTime() == null ? null : loginInfo.getLoginTime().getTime());
			on.setOs(loginInfo.getOs());
			on.setTokenId(token.substring(token.lastIndexOf(":") + 1)); //获取UUID值即可，不需要前缀
			
			userOnlineList.add(on);
		}
		Collections.reverse(userOnlineList);
		userOnlineList.removeAll(Collections.singleton(null));
		return toPage(userOnlineList);
	}

	/**
	 * 强退用户
	 */
	@Log(title = "在线用户", businessType = BusinessType.FORCE)
	@DeleteMapping("/{tokenId}")
	@SaCheckPermission("monitor:online:forceLogout")
	public void forceLogout(@PathVariable String tokenId) {
		StpUtil.logoutByTokenValue(tokenId);
	}
}
